package org.example.zhihuan.control;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.zhihuan.services.Administrators;
import org.example.zhihuan.services.Assessment;
import org.example.zhihuan.services.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.example.zhihuan.control.Result.PARAMETER_WRONG;
import static org.example.zhihuan.control.Result.USER_BAN;

@WebServlet(name = "AdminServlet",urlPatterns = "/administrators")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Content-Type","application/json;charset=UTF-8");

        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
        Map<String,Object> map = mapper.readValue(req.getInputStream(), HashMap.class);
        try{
            switch (map.get("_type").toString()){
                case "createAdmin":
                    createAdmin(map,resp);
                    break;
                case "adminCheck":
                    adminCheck(map,resp);
                    break;
                case "adminCheckAss":
                    adminCheckAss(map,resp);
                    break;
                case "adminCheckComp":
                    adminCheckComp(map,resp);
                    break;
                case "adminBan":
                    adminBan(map,resp);
                    break;
                case "adminAssDelete":
                    adminAssDelete(map,resp);
                    break;
                case "adminGoodsDown":
                    adminGoodsDown(map,resp);
                    break;
                case "adminUnline":
                    adminUnline(map,resp);
                    break;
                default:
                    resp.getWriter().print(new Result(999,"未知",null));
            }
        }catch (Exception e){
            resp.getWriter().print(new Result(432,"参数不正确",null));
        }
    }


    /**
     * 管理员下线
     * @param map
     * @param resp
     * @throws IOException
     */
    private void adminUnline(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!map.containsKey("adminId")){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }
        Administrators admin = Administrators.getAdmin((Integer)map.get("adminId"));
        admin.adminUnLine(admin.getAdminId());
        Result result;
        result = Result.ok(map.get("adminId"));
        resp.getWriter().print(result);
    }

    /**
     * 管理员下架商品
     * @param map
     * @param resp
     * @throws IOException
     */
    private void adminGoodsDown(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!(map.containsKey("goodsId") && map.containsKey("adminId"))){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }
        Administrators admin = Administrators.getAdmin((Integer)map.get("adminId"));
        admin.adminGoodsDown((Integer)map.get("goodsId"));
        Result result;
        result = Result.ok(map.get("goodsId"));
        resp.getWriter().print(result);
    }

    /**
     * 管理员删除评论
     * @param map
     * @param resp
     * @throws IOException
     */
    private void adminAssDelete(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!(map.containsKey("assId") && map.containsKey("adminId"))){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }
        Administrators admin = Administrators.getAdmin((Integer)map.get("adminId"));
        admin.adminAssDelete((Integer)map.get("assId"));
        Result result;
        result = Result.ok(map.get("assId"));
        resp.getWriter().print(result);
    }

    /**
     * 处理封禁账号
     * @param map
     * @param resp
     * @throws IOException
     */
    private void adminBan(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!(map.containsKey("ban") && map.containsKey("userId") && map.containsKey("adminId"))){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }
        Administrators admin = Administrators.getAdmin((Integer)map.get("adminId"));
        admin.adminBan((boolean) map.get("ban"),(Integer)map.get("userId"));
        Result result;
        result = Result.ok(map.get("userId"));
        resp.getWriter().print(result);
    }

    /**
     * 管理员审核投诉
     * @param map
     * @param resp
     * @throws IOException
     */
    private void adminCheckComp(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!(map.containsKey("checkComp") && map.containsKey("compId") && map.containsKey("adminId"))){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }
        Result result;
        Administrators admin = Administrators.getAdmin((Integer) map.get("adminId"));
        admin.adminCheckComp((boolean)map.get("checkComp"),(Integer) map.get("compId"));
        result = Result.ok(map.get("compId"));
        resp.getWriter().print(result);
    }

    /**
     * 管理员审核评论
     * @param map
     * @param resp
     * @throws IOException
     */
    private void adminCheckAss(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!(map.containsKey("checkAss") && map.containsKey("assId") && map.containsKey("adminId"))){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }
        Result result;
        Administrators admin = Administrators.getAdmin((Integer) map.get("adminId"));
        admin.adminCheckAss((boolean)map.get("checkAss"),(Integer) map.get("assId"));
        result = Result.ok(map.get("assId"));
        resp.getWriter().print(result);
    }

    /**
     * 管理员审核商品
     * @param map
     * @param resp
     * @throws IOException
     */
    private void adminCheck(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!(map.containsKey("check") && map.containsKey("goodsId") && map.containsKey("adminId"))){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }
        Result result;
        Administrators admin = Administrators.getAdmin((Integer) map.get("adminId"));
        admin.adminCheck((boolean)map.get("check"),(Integer) map.get("goodsId"));
        result = Result.ok(map.get("goodsId"));
        resp.getWriter().print(result);
    }

    /**
     * 管理员创建
     * @param map
     * @param resp
     * @throws IOException
     */
    private void createAdmin(Map<String, Object> map, HttpServletResponse resp) throws IOException{
        if (!(map.containsKey("adminName") && map.containsKey("adminPsw"))){
            resp.getWriter().print(new Result(PARAMETER_WRONG,"参数不正确",null));
        }

        Administrators admin = Administrators.createAdmin(map.get("adminName").toString(),map.get("adminPwd").toString());
        Result result;
        if (admin == null){
            result = Result.fail(Result.NOT_ALLOW,"未能创建管理员对象",null);
        }else {
            result = Result.ok(admin.getAdminId());
        }
        resp.getWriter().print(result);
    }
}
